#include "Player.h"

using namespace car;

Player::Player(void)
{
	mState = PlayerStateNone;
	mPlayerId = 0;
	mPlayerNameSize = 0;
	memset( &mPlayerName[0], 0, sizeof( mPlayerName ) );
	mRoomId = 0;
	memset( &mAttribute, 0, sizeof( mAttribute ) );
	mLoadingProgress = 0;
	mGameResult = GameResultDraw;
	mScore = 0;
	mPacketSent = false;
	mCorrection = false;
}

Player::Player( unsigned int playerId )
{
	mState = PlayerStateNone;
	mPlayerId = playerId;
	mPlayerNameSize = 0;
	memset( &mPlayerName[0], 0, sizeof( mPlayerName ) );
	mRoomId = 0;
	memset( &mAttribute, 0, sizeof( mAttribute ) );
	mLoadingProgress = 0;
	mGameResult = GameResultDraw;
	mScore = 0;
	mPacketSent = false;
	mCorrection = false;
}

Player::~Player(void)
{
}

void Player::getPlayerName( char * name, unsigned int * size )
{
	if( name )
	{
		memcpy( name, &mPlayerName[0], mPlayerNameSize );
		*size = mPlayerNameSize;
	}
}

void Player::setPlayerName( const char * name, unsigned int size )
{
	if( size > 0 && size < kMaxPlayerNameLength )
	{
		memcpy( &mPlayerName[0], name, size );
		mPlayerNameSize = size;
	}
}

void Player::enterRoom( unsigned int roomId )
{
	if( mState == PlayerStateInHall )
	{
		mState = PlayerStateInRoom;
		mRoomId = roomId;
	}
}

void Player::leaveRoom()
{
	if( mState == PlayerStateInRoom )
	{
		mState = PlayerStateInHall;
		mRoomId = 0;
	}
}

void Player::updateGamePlayStates( const PlayerAttribute & attr )
{
	if( mState == PlayerStateInGame )
	{
		mAttribute.posX = attr.posX;
		mAttribute.posY = attr.posY;
		mAttribute.velX = attr.velX;
		mAttribute.velY = attr.velY;
		mAttribute.timeStamp = attr.timeStamp;
		if( attr.itemInUse != ItemTypeNone )
		{
		}
	}
}

bool Player::sendPacket( const unsigned char data[], int size )
{
	return mConnection.sendPacket( data, size );
}

int Player::receivePacket( unsigned char data[], int size )
{
	LASAddress addr;
	return mConnection.receivePacket( addr, data, size );

}

void Player::resetStates()
{
	mState = PlayerStateNone;
}